<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 显示部门结构 -->
        <el-tree
          :data="departs"
          :props="treeProps"
          :default-expand-all="true"
          :highlight-current="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @addDepts="addDepts"
            @editDepts="editDepts"
            @update-list="getDepartments"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'

export default {
  components: {
    TreeTools,
    AddDept
  },
  data () {
    return {
      departs: [],
      treeProps: {
        children: 'children',
        label: 'name'
      },
      company: {},
      showDialog: false,
      node: null
    }
  },
  created () {
    this.getDepartments() // 调用方法
  },
  methods: {
    handleNodeClick (obj) {
      // 节点被点击时的回调
      console.log(obj)
    },
    async getDepartments () {
      const result = await getDepartments()
      this.departs = tranListToTreeData(result.depts, '')
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      // console.log(result)
    },
    addDepts (node) {
      this.showDialog = true
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true
      this.node = node
      // 获取部门详情
      // console.log(this.$refs.addDept)
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
  font-size: 14px;

  .sp-title {
    display: flex;
    align-content: center;

    .el-icon-office-building {
      font-size: 16px;
      margin-right: 5px;
    }
  }
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
